package com.abso.sunlight.api;

import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.dom4j.Element;

/**
 * Congressional district.
 */
public class District {

    /** The district's state. */
    private State state;

    /** The district's number. */
    private int number;

    /**
     * Constructs a new district.
     * 
     * @param elem
     *            the XML element.
     * @throws SunlightException
     *             if an error occurred creating the district.
     */
    public District(Element elem) throws SunlightException {
        this.state = State.valueOfAbbreviation(SunlightDataUtils.getString(elem, "state"));
        this.number = SunlightDataUtils.getInt(elem, "number");
    }

    /**
     * Returns the districts's state.
     * 
     * @return the district's state.
     */
    public State getState() {
        return state;
    }

    /**
     * Returns the districts's number.
     * 
     * @return the district's number.
     */
    public int getNumber() {
        return number;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
    }

    @Override
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this);
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof District)) {
            return false;
        }
        District district = (District) obj;
        return (getState() == district.getState()) && (getNumber() == district.getNumber());
    }

}
